<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<style>
    .form-group{
        margin-bottom: 0;
    }
</style>
<head th:include="include/head">

</head>

<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">


    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1>题库管理</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">首页</a></li>
                            <li class="breadcrumb-item active">题库管理</li>
                        </ol>
                    </div>
                </div>
            </div>
            <!-- /.container-fluid -->
        </section>
        <section class="content">
            <form id="fileupload" th:action="@{/common/upload}" method="post"
                  style="display: none;">
                <input type="hidden"  id="type" />
                <input type="file" name="file" id="file" />
            </form>
            <form class="form-horizontal" id="form" method="post"
                  enctype="multipart/form-data" th:action="@{/library/save}">

                <div class="card card-info">

                    <div class="card-header">
                        <h3 class="card-title">基本信息</h3>
                    </div>

                    <input type="hidden" name="libraryId" id="libraryId" th:value="${bean.libraryId}" />
                    <div class="card-body">
                        <div class="form-group row">
                            <label for="libraryName" class="col-sm-2 col-form-label">标题</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="libraryName" required="required"
                                       id="libraryName" aria-required="true" th:value="${bean.libraryName}">
                                <label class="help-block m-b-none" for="libraryName"
                                       id="libraryName-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>


                        <div class="form-group row">
                            <label for="baseId" class="col-sm-2 col-form-label">所属部门</label>
                            <div class="col-sm-10">
                                <select  class="form-control"   id="baseId" name="baseId"  required>
                                    <option value="0">选择部门</option>
                                    <option  th:each="model : ${baseList}" th:value="${model.baseId}"
                                             th:text="${model.baseName}" th:selected="${model.baseId}==${bean.baseId}" value="0">否</option>
                                </select>
                                <input   type="hidden"  name="baseName"  id="baseName">
                                <label class="help-block m-b-none" for="baseId"
                                       id="baseId-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="projectId" class="col-sm-2 col-form-label">项目</label>
                            <div class="col-sm-10">
                                <select  class="form-control"   id="projectId" name="projectId"  required>
                                    <option value="0">全部门</option>
                                    <option  th:each="model : ${projectList}" th:value="${model.projectId}"
                                             th:text="${model.projectName}" th:selected="${model.projectId}==${bean.projectId}" value="0">否</option>
                                </select>
                                <input   type="hidden"  name="projectName"  id="projectName">
                                <label class="help-block m-b-none" for="projectId"
                                       id="projectId-error"><i class="far fa-bell"></i>必填项</label>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="libraryState" class="col-sm-2 col-form-label">状态</label>
                            <div class="col-sm-10">
                                <input type="checkbox" class="js-switch" id="state3"  th:checked="${bean.libraryState}==1"  />
                                <input type="hidden" id="libraryState" name="libraryState" th:value="${bean.libraryState}">
                            </div>
                        </div>
                    </div>
                </div>


            </form>
            <!-- /.card-body -->
            <div class="card-footer">
                <button type="button" class="btn btn-info" onclick="validaBef();">保存</button>
                <button type="button" class="btn btn-default float-right" id="back">取消</button>
            </div>
            <!-- /.card-footer -->


    </section>
    </div>
</div>
<!-- ./wrapper -->
<div th:include="include/foot"></div>

<script>
    var issubmit=false;
    function dosubmit(){
        if(issubmit==false){
            issubmit=true;
            return true;
        }else{
            return false;
        }
    }
    $(document).on("click",".del",function() {
        $(this).parent().parent().remove();

    });
    $('#fileType').change(function (){
        let  type=$(this).val()
        if (type==1){
            $('#attr').show()
        }else{
            $('#attr').hide()
        }
    })
    $(document).on("click",".delfilediv",function() {
        $(this).parent().remove();

    });
    $("#add_data").click(function(){
        layer.open({
            type: 2,
//		        closeBtn:0,
            title: false,
            area: ['50%', '95%'],
            content: 'add_data',
        });
    });
    $("#addFile").click(function(){
        $("#file").click();
    });
    $("#back").click(function() {
        location.href = 'list';
    });
    $("#baseId").change(function (){
        $("#baseName").val($('#baseId  option:selected').text())
    })
    $("#baseName").val($('#baseId  option:selected').text())
    $("#file").change(function(e){
        if($(this).val()!=''){
            $("#fileupload").ajaxSubmit({
                success:function(e){
                    console.log(e);
                    if(e.res){
                        let filediv= $(".filediv2:eq(0)");
                        // filedivclone.find('img').attr('src',e.url);
                        filediv.find('a').html("<i class='fas fa-paperclip'></i>"+e.url);
                        filediv.parent().find('.picUrl').attr('value',e.url);
                        $("#file").val("")
                    }

                },error:function(e){
                    layer.msg(e.responseText, {icon: 5});
                }
            });
        }

    });
    // 	var elem = document.querySelector('.js-switch');
    // 	var switchery = new Switchery(elem, { color: '#1AB394' });
    var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
    elems.forEach(function(html) {
        var switchery = new Switchery(html, { color: '#1AB394' });
    });

    $.validator.setDefaults({
        ignore: "",
        highlight : function(a) {
            $(a).removeClass("is-valid").addClass("is-invalid");
        },
        success : function(a) {
            $(a).prev().removeClass("is-invalid").addClass("is-valid");
            $(a).html('<i class="fas fa-check"></i>验证成功');
        },
        errorClass : "help-block m-b-none",
// 			validClass : "help-block m-b-none",
        errorPlacement : function(a, b) {
            if (b.is(":radio") || b.is(":checkbox")) {
                a.appendTo(b.parent().parent().parent())
            } else {
                a.appendTo(b.parent())
            }
        },
        submitHandler:function(form){

            form.submit();
        }
    });
    $.validator.addMethod("checkFile",function(value,element){
        let files=$("input[name='fileurls']");
        if(files.length==0){
            return true;
        }
        return false;
    },"");
    $.validator.addMethod("mobile", function(value, element) {
        var myreg=/^[1][3,4,5,6.7,8,9][0-9]{9}$/;
        var length = value.length;
        return this.optional(element) || (length == 11 && myreg.test(value));
    }, "手机号码格式错误!");
    $("#form").validate(
        {
            ignore : "",
            rules : {
                goodsName : "required",
                doctorPhone: {
                    required:true,
                    mobile:true
                },
                filehidden : {
                    required:true
                },
                attrs : {
                    required:true
                },

            },
            messages : {
                goodsName : '<i class="far fa-times-circle"></i>请输入商品名称',
                filehidden : '<i class="far fa-times-circle"></i>请上传图片',
                attrs : '<i class="far fa-times-circle"></i>请选择关联专家',
            }
        });
    function validaBef(){


        if($("#state3").prop("checked")){
            $("#libraryState").val("1");
        }else{
            $("#libraryState").val("0");
        }
        if ($("input[name='eId']").length>0){
            $("#attrs").attr('value',$("input[name='eId']").length);
        }


        if($("#form").validate()){
            if(dosubmit()){
                $("#form").submit();
            }
            setTimeout(function () {
                issubmit=false
            }, 3000);
        }

    }
    $(document).ready(function (){
        $("#hospitalId").change(function (){
            let v=$(this).val();
            let t= $(this).find("option:selected").text();
            if(v>0){
                $("#hospitalName").val(t);
            }
            $.ajax({url:'../doctor/departmentList',data:"hospitalId="+v,success:function (data){
                    if(data.res){
                        $("#departmentId").empty();
                        $("#departmentId").append("<option value='-1'>请选择科室</option>");
                        for(let i=0;i<data.list.length;i++){
                            console.log(data.list[i])
                            $("#departmentId").append("<option value='"+data.list[i].id+"'>"+data.list[i].departmentName+"</option>");
                        }

                    }
                }})
        });
        $("#departmentId").change(function (){
            let v=$(this).val();
            let t= $(this).find("option:selected").text();
            console.log(v,t)
            if(v>0){
                $("#departmentName").val(t);
            }

        });
        $("#projectId").change(function (){
            let v=$(this).val();
            let t= $(this).find("option:selected").text();
            if(v>0){
                $("#projectName").val(t);
            }
        });
        $("#baseId").change(function (){
            let v=$(this).val();
            let t= $(this).find("option:selected").text();
            console.log(v,t)
            if(v>0){
                $("#baseName").val(t);
                $.ajax({url:'../library/projectList',data:"baseId="+v,success:function (data){
                        console.log(data)
                        if(data.res){
                            $("#projectId").empty();
                            $("#projectId").append("<option value='0'>全部门</option>");
                            for(let i=0;i<data.list.length;i++){
                                console.log(data.list[i])
                                $("#projectId").append("<option value='"+data.list[i].projectId+"'>"+data.list[i].projectName+"</option>");
                            }

                        }
                    }})
            }else {
                $("#projectId").empty();
                $("#projectId").append("<option value='0'>全部门</option>");
            }

        });
    });

</script>
</body>
</html>